package cn.inst.common;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import org.springframework.stereotype.Service;

import com.alibaba.druid.util.Base64;
@Service
public class SecurityUntils {
	
	/**
	 * MD5加密算法
	 * @param password
	 * @return
	 * @throws NoSuchAlgorithmException 
	 * @throws UnsupportedEncodingException 
	 */
	public static String MD5Password(String password) throws NoSuchAlgorithmException, UnsupportedEncodingException{
		MessageDigest md5=MessageDigest.getInstance("MD5");
		String md5Password = Base64.byteArrayToAltBase64(md5.digest(password.getBytes("utf-8")));
		return md5Password;
	}
	
	/**
	 * 测试输入密码与数据库是否一致
	 * @param inputPw
	 * @param DbPw
	 * @return
	 * @throws UnsupportedEncodingException 
	 * @throws NoSuchAlgorithmException 
	 */
	public static boolean checkPassword(String inputPw,String DbPw) throws NoSuchAlgorithmException, UnsupportedEncodingException{
		
		return MD5Password(inputPw).equals(DbPw);
		
	}
}
